<?php
class Frames_Form extends Zend_Form
{
	const FORM_CLASS_NAME = 'form ui-form-default';
	
	
	const ERROR_CLASS_NAME = 'ui-state-error'; 
	const ELEMENT_CLASS_NAME = 'ui-widget-content ui-corner-all'; 
	
	
	
	protected $legendLabel;
	
	public function setLegendLabel( $val )
	{
		$this->legendLabel = (string)$val;
		return $this;
	}
	
	public function getErrorClassName( )
	{
		return self::ERROR_CLASS_NAME;
	}
	
	public function render( Zend_View_Interface $view = null)
	{
		$content = parent::render($view);
		$content = str_replace('<legend>', '<legend class="ui-widget ui-widget-header ui-corner-all" >', $content);
		return $content;
	}
	
	
	public function __construct($options = null)
	{
		$options[ 'legendLabel' ] = (isset($options[ 'legendLabel' ])) ?  $options[ 'legendLabel' ] : 'צור קשר';
	 	$this->setDisableLoadDefaultDecorators( true )
	 			->setAttrib('id', get_class( $this ))
	 			->setAttrib('class', self::FORM_CLASS_NAME);
	 	parent::__construct( $options );
	 	$this->addDecorator('FormElements')
				->addDecorator('Fieldset',array('class' => 'ui-widget ui-widget-content ui-corner-all','legend' => $options[ 'legendLabel' ]))
     		 	->addDecorator('Form');
		$_view = $this->getView( );
		$baseUrl = $_view->baseUrl();
		$_view->headLink()->appendStylesheet( $baseUrl.'/css/form.css');
		$_view->headScript( )->appendFile( $baseUrl.'/js/Sb/Form.js' )
					->appendFile( $baseUrl.'/js/Sb/Form/Decorator.js' )
					->appendFile( $baseUrl.'/js/Sb/Form/Validation.js' );
	}
	
	public function init(  )
	{
		$formElements = $this->getElements( );
		$decorators = array(
    			array('ViewHelper'), 
    			array('Errors',array('class' => self::ERROR_CLASS_NAME)), 
				/*array('Label',
					array('requiredSuffix' => ' * '
				)),*/
    			array('Label'),
				array('HtmlTag',
					array('tag' => 'div'))
        );
		$sbForm = array( );			
		foreach ($formElements as $elmName => $elmObj){
			$sbFormParams = array(
				'id' 				=> $elmObj->getId( ),
				'msg' 			=> $elmObj->getErrorMessages( ),
				'validators' 	=> array( )
			);
			if($elmObj->isRequired( )){
				$sbFormParams[ 'validators' ][ ] = array(
					'name' => 'required',
					'params' => array( )
				); 
			}
			
			$validators = $elmObj->getValidators( );
			if($validators){
				foreach($validators as $name => $obj){
					$validatorParams = array( );
					if($messageVariables = $obj->getMessageVariables()){
						foreach($messageVariables as $variable){
							if(!is_null($obj->$variable)){
								$validatorParams[ ] = $obj->$variable;
							}	
						}
					}
					$sbFormParams[ 'validators' ][ ] = array(
						'name' 		=> lcfirst( str_replace('Zend_Validate_','',$name)),
						'params' 	=> $validatorParams
					);
					//Sb_Debug::debug($sbFormParams[ 'validators' ]);
				}
			}
			$sbForm[ ] = $sbFormParams; 
			$typeArr = explode('_',$elmObj->getType( ));
			$type = strtolower($typeArr[count($typeArr)-1]);
			$elmObj->setAttrib('class',$type.' '.self::ELEMENT_CLASS_NAME)
					 ->setDecorators( $decorators );
		}
		$submitName = 'submit'.$this->getAttrib( 'id' );
		$this->addElement('submit', $submitName,
			 array( 'label'    => 'שלח',
			 'class' => 'submit ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
		) );
		
		
        $this->getElement($submitName)
        	 ->setDecorators(array(array('ViewHelper'),
             	array('HtmlTag', array('tag' => 'div'))
        	  ));
		
		$this->getView( )->headScript()->appendScript( '$(function(){var _form = new Sb_Form( "'.$this->getAttrib( 'id' ).'",'.Zend_Json::encode( $sbForm ).' )})' );
	}
	
}